package papacharly.encheres.client;

/**
 *
 * @author Rudy
 */
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import papacharly.encheres.idl.*;
import papacharly.encheres.log.Logger;

public class ClientWrapper {

    private static final String SERVER_PORT = "1050";
    private static final String SERVER_HOST = "localhost";
    private static final String[] INIT_PARAM = {"-ORBInitialPort", SERVER_PORT, "-ORBInitialHost", SERVER_HOST};
    private static final String SERVER_NAME = "Enchere";
    private Logger log = Logger.getLogger(this.getClass().getSimpleName());
    private Serveur serveurCorba = null;
    private papacharly.encheres.idl.Client clientCorba = null;

    public ClientWrapper() {
        try {
            // create and initialize the ORB
            ORB orb = ORB.init(INIT_PARAM, null);

            POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
            rootPOA.the_POAManager().activate();

            // get the root naming context
            org.omg.CORBA.Object objRef =
                    orb.resolve_initial_references("NameService");
            // Use NamingContextExt instead of NamingContext. This is
            // part of the Interoperable naming Service.
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            /*
             * ClientWrapper en serveur pour attente du notify
             */
            ClientImpl clientImpl = new ClientImpl();
            clientImpl.setOrb(orb);

            // get object reference from the servant
            org.omg.CORBA.Object ref = rootPOA.servant_to_reference(clientImpl);
            clientCorba = ClientHelper.narrow(ref);


            /*
             * Recherche du serveur et appel
             */
            serveurCorba = ServeurHelper.narrow(ncRef.resolve_str(SERVER_NAME));
            log.debug("Obtained a handle on server object: " + serveurCorba);

            // Abonnement au serveur d'encheres
            int[] lstProduits = {1, 2};
            serveurCorba.subscribe(clientCorba, lstProduits);
//            orb.shutdown(false);

        } catch (Exception e) {
            log.error("Impossible d'initialiser le client CORBA", e);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Getters">
    public papacharly.encheres.idl.Client getClientCorba() {
        return clientCorba;
    }

    public Serveur getServeurCorba() {
        return serveurCorba;
    }// </editor-fold>
}
